<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>市场新闻 - 低位龙头股分析系统</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.0/font/bootstrap-icons.css">
    <style>
        .header {
            background-color: #343a40;
            color: white;
            padding: 2rem 0;
            margin-bottom: 2rem;
        }

        .card {
            margin-bottom: 1.5rem;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }

        .card-header {
            background-color: #f8f9fa;
            font-weight: bold;
        }

        .news-item {
            padding: 15px;
            border-bottom: 1px solid #eee;
        }

        .news-item:last-child {
            border-bottom: none;
        }

        .news-time {
            color: #6c757d;
            font-size: 0.9rem;
        }

        .news-source {
            color: #28a745;
            font-weight: bold;
        }

        .news-title {
            font-size: 1.1rem;
            font-weight: bold;
            margin-bottom: 8px;
        }

        .news-content {
            color: #333;
        }

        .update-info {
            font-size: 0.9rem;
            color: #6c757d;
            margin-top: 10px;
        }

        .loading {
            display: none;
            text-align: center;
            padding: 20px;
        }
    </style>
</head>

<body>
    <div class="header">
        <div class="container">
            <h1>市场新闻</h1>
            <p>了解最新的市场动态和财经新闻</p>
        </div>
    </div>

    <div class="container">
        <div class="row mb-4">
            <div class="col-12">
                <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
                    <div class="container-fluid">
                        <button class="navbar-toggler" type="button" data-bs-toggle="collapse"
                            data-bs-target="#navbarNav">
                            <span class="navbar-toggler-icon"></span>
                        </button>
                        <div class="collapse navbar-collapse" id="navbarNav">
                            <ul class="navbar-nav">
                                <li class="nav-item">
                                    <a class="nav-link" href="/">首页</a>
                                </li>
                                <li class="nav-item">
                                    <a class="nav-link" href="/results">历史结果</a>
                                </li>
                                <li class="nav-item">
                                    <a class="nav-link" href="/backtesting">回测系统</a>
                                </li>
                                <li class="nav-item">
                                    <a class="nav-link active" href="/news">市场新闻</a>
                                </li>
                            </ul>
                        </div>
                    </div>
                </nav>
            </div>
        </div>

        <div class="row mb-4">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header d-flex justify-content-between align-items-center">
                        <h5 class="mb-0">新闻筛选</h5>
                        <button id="update-news" class="btn btn-sm btn-success">
                            <i class="bi bi-arrow-repeat"></i> 更新新闻
                        </button>
                    </div>
                    <div class="card-body">
                        <form id="filter-form" method="get" action="/news">
                            <div class="row">
                                <div class="col-md-5">
                                    <div class="mb-3">
                                        <label for="days" class="form-label">显示最近几天的新闻</label>
                                        <select class="form-select" id="days" name="days">
                                            <option value="1" {% if days==1 %}selected{% endif %}>1天</option>
                                            <option value="3" {% if days==3 %}selected{% endif %}>3天</option>
                                            <option value="7" {% if days==7 %}selected{% endif %}>1周</option>
                                            <option value="14" {% if days==14 %}selected{% endif %}>2周</option>
                                            <option value="30" {% if days==30 %}selected{% endif %}>1个月</option>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-md-5">
                                    <div class="mb-3">
                                        <label for="keywords" class="form-label">关键词搜索</label>
                                        <input type="text" class="form-control" id="keywords" name="keywords"
                                            value="{{ keywords }}" placeholder="输入关键词，多个关键词用逗号分隔">
                                    </div>
                                </div>
                                <div class="col-md-2 d-flex align-items-end">
                                    <div class="mb-3 w-100">
                                        <button type="submit" class="btn btn-primary w-100">筛选</button>
                                    </div>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>

        <div class="row">
            <div class="col-md-12">
                <div class="card">
                    <div class="card-header">
                        <h5 class="mb-0">市场新闻列表</h5>
                    </div>
                    <div class="card-body p-0">
                        <div id="loading" class="loading">
                            <div class="spinner-border text-primary" role="status">
                                <span class="visually-hidden">Loading...</span>
                            </div>
                            <p class="mt-2">正在更新新闻，请稍候...</p>
                        </div>

                        <div id="news-list">
                            {% if news_items %}
                            {% for news in news_items %}
                            <div class="news-item">
                                <div class="news-title">
                                    <a href="{{ news.url }}" target="_blank">{{ news.title }}</a>
                                </div>
                                <div class="news-content">{{ news.content }}</div>
                                <div class="d-flex justify-content-between mt-2">
                                    <div class="news-source">{{ news.source }}</div>
                                    <div class="news-time">{{ news.publish_time }}</div>
                                </div>
                            </div>
                            {% endfor %}
                            {% else %}
                            <div class="p-4 text-center">
                                <p>没有找到符合条件的新闻</p>
                                <button id="update-empty" class="btn btn-outline-primary">
                                    <i class="bi bi-arrow-repeat"></i> 更新新闻
                                </button>
                            </div>
                            {% endif %}
                        </div>

                        {% if news_items %}
                        <div class="update-info text-end p-3">
                            共 {{ news_items|length }} 条新闻
                        </div>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        $(document).ready(function () {
            // 更新新闻按钮点击事件
            $('#update-news, #update-empty').click(function (e) {
                e.preventDefault();

                // 显示加载中
                $('#loading').show();
                $('#news-list').hide();

                // 发送更新请求
                $.ajax({
                    url: '/update_news',
                    type: 'POST',
                    success: function (response) {
                        if (response.success) {
                            // 刷新页面
                            window.location.reload();
                        } else {
                            alert('更新新闻失败: ' + response.message);
                            $('#loading').hide();
                            $('#news-list').show();
                        }
                    },
                    error: function () {
                        alert('服务器错误，请稍后再试');
                        $('#loading').hide();
                        $('#news-list').show();
                    }
                });
            });

            // 表单提交时如果关键词为空，移除关键词参数
            $('#filter-form').submit(function () {
                if ($('#keywords').val().trim() === '') {
                    $('#keywords').prop('name', '');
                }
            });
        });
    </script>
</body>

</html>